package com.task.web.controller.core;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import com.task.common.constant.ErrorCodeConstants;
import com.task.common.exception.ServiceException;
import com.task.core.dto.request.SysFilesRequestDTO;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.task.common.core.controller.BaseController;
import com.task.common.core.domain.AjaxResult;
import com.task.common.core.page.TableDataInfo;
import com.task.common.utils.SecurityUtils;
import com.task.core.domain.SysFileInfo;
import com.task.core.dto.request.SysFileRequestDTO;
import com.task.core.service.SysFileService;

import io.swagger.annotations.Api;

/**
 * @author 迪迦.
 * @date 2024/9/14 14:11
 */
@Api("文件管理")
@RestController
@RequestMapping("/file")
public class SysFileController extends BaseController {

  @Resource
  private SysFileService fileService;

  @PostMapping("/upload")
  public AjaxResult upload(SysFileRequestDTO request) throws IOException, URISyntaxException {
    return success(fileService.upload(request, SecurityUtils.getLoginUser().getUserId()));
  }

  @PostMapping("/uploadScript")
  public AjaxResult uploadScript(SysFileRequestDTO request) throws IOException, URISyntaxException, NoSuchAlgorithmException {
    if (null == request.getFile() || request.getFile().isEmpty()) {
     return error("上传文件内容不能为空");
    }
    return success(fileService.uploadScript(request, SecurityUtils.getLoginUser().getUserId()));
  }

  @PostMapping("/uploadScriptType")
  public AjaxResult uploadScriptType(SysFilesRequestDTO request) throws IOException, URISyntaxException, NoSuchAlgorithmException {
    if (ObjectUtils.isEmpty(request.getFile())) {
      return error(ErrorCodeConstants.UPLOAD_FILE_CONTENT_IS_NOT_EMPETY.getMsg());
    }
    SysFileInfo fileInfo = fileService.uploadScriptType(request);
    return success(fileInfo);
  }

  @GetMapping("/download/{code}")
  public void download(@PathVariable String code, HttpServletResponse response)
      throws IOException {
    fileService.download(code, response);

  }

  @GetMapping("/getFilesByFromCode/{fromCode}")
  public TableDataInfo getFilesByFromCode(@PathVariable String fromCode) {
    return getDataTable(fileService.getFilesByFromCode(fromCode));
  }

  @GetMapping("/getFilesByPage")
  public TableDataInfo getFilesByPage(@RequestBody SysFileRequestDTO request) {
    return getDataTable(fileService.getFilesByPage(request));
  }

}
